package com.bndg.shu.shuyinyangshi.engine.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.bndg.shu.shuyinyangshi.bean.MonsterBean;
import com.bndg.shu.shuyinyangshi.engine.DataBaseHelper;
import com.orhanobut.logger.Logger;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.TreeMap;

import static com.bndg.shu.shuyinyangshi.MyApp.mContext;

/**
 * Created by r on 17/3/22.
 */

public class UseDataBase {
    private static final String DB_NAME = "shuyys.db";
    public static final String YYS_TABLE = "yysdb";
    int[] ints = {5, 8, 11, 14, 17, 20};
    public SQLiteDatabase sqlitedatabase;
    private boolean isOpen;

    //打开数据库连接
    public void opendb() {
        sqlitedatabase = openDataBase();
    }

    public synchronized void closedb() {
        if (sqlitedatabase != null)
            sqlitedatabase.close();
        isOpen = false;
    }


    //插入表数据
    public void insert(String table_name, ContentValues values) {
        opendb();
        sqlitedatabase.insert(table_name, null, values);
        closedb();
    }

    //更新数据
    public int updatatable(String table_name, ContentValues values, int ID) {
        opendb();
        return sqlitedatabase.update(table_name, values, " Type_ID = ? ", new String[]{String.valueOf(ID)});
    }

    //删除表数据
    public void delete(String table_name) {
        opendb();
        try {

            sqlitedatabase.delete(table_name, null, null);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closedb();
        }
    }

    /**
     * 查询指定id的数据
     */
    public ArrayList<MonsterBean> query(String str) {
        String sql = "_acronym1 LIKE ? or _acronym2 LIKE ?  or _acronym3 LIKE ? " +
                " or _acronym4 LIKE ?  or _acronym5 LIKE ?  or _acronym6 LIKE ?";
        String[] args = {"%" + str + "%", "%" + str + "%", "%" + str + "%",
                "%" + str + "%", "%" + str + "%", "%" + str + "%"};
        Cursor cursor = sqlitedatabase.query(YYS_TABLE, null,
                sql, args, null, null, null);
        ArrayList<MonsterBean> monsterList = new ArrayList<>();
        int current = 0;
        while (cursor.moveToNext()) {
            String title = cursor.getString(1) + " " + cursor.getString(2);
            MonsterBean monsterBean;
            if (monsterList.size() != 0 && title.equals(monsterList.get(current - 1).title)) {
                monsterBean = monsterList.get(current - 1);
            } else {
                monsterBean = new MonsterBean();
                monsterBean.contents = new LinkedHashMap<>();
                monsterList.add(monsterBean);
                current++;
            }
            monsterBean.title = title;
            String monster = cursor.getString(3);
            for (int i = 0; i < ints.length; i++) {
                String string = cursor.getString(ints[i]);
                if (!TextUtils.isEmpty(string) && string.contains(str.toUpperCase())) {
                    String content;
                    String amount;
                    if ("神秘线索".equals(monster)) {
                        monster = cursor.getString(4);
                        content = cursor.getString(ints[i] + 2);
                        amount = cursor.getString(ints[i] + 3);
                    } else {
                        content = cursor.getString(ints[i] - 1);
                        amount = cursor.getString(ints[i] + 1);
                    }

                    monsterBean.contents.put(monster, content + "x" + amount);
                }
            }
        }
        cursor.close();
        return monsterList;
    }

    public SQLiteDatabase openDataBase() {
        isOpen = true;
        //Open the database
        return SQLiteDatabase.openDatabase(
                mContext.getFilesDir().getAbsolutePath() + File.separator + DB_NAME,
                null, SQLiteDatabase.OPEN_READONLY);
    }
}
